package com.zhongke.cotrun.service.impl;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zhongke.cotrun.service.AgentInfoService;
import com.zhongke.cotrun.service.AgentJobsService;
import com.zhongke.cotrun.service.RegionService;
import com.zhongke.cotrun.service.UserInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.util.StringUtil;
import com.zhongke.cotrun.mapper.AgentInfoMapper;
import com.zhongke.cotrun.model.pojo.AgentInfo;
import com.zhongke.cotrun.model.pojo.Region;
import com.zhongke.cotrun.model.pojo.UserInforCompany;

/**
 * @Description: 经纪人附加信息
 * @Author: songjing
 * @Date:   2020-03-28
 * @Version: V1.0
 */
@Service
public class AgentInfoServiceImpl extends ServiceImpl<AgentInfoMapper, AgentInfo> implements AgentInfoService {
	@Autowired
	AgentInfoMapper agentInfoMapper;
	@Autowired
	AgentJobsService agentJobsService;
	@Autowired
	UserInfoService userInfoService;
	@Autowired
	RegionService regionService;

	/**
	 * 前端检索经纪人信息
	 * @param showPublic 是否公开
	 * @param skillArea 技术领域
	 * @param researchArea 关注行业
	 * @param province 省
	 * @param city 市
	 * @param pageNum
	 * @param pageSize
	 * @return
	 */
	@Override
	public PageInfo selectAll(String showPublic,String skillArea, String researchArea,String province, String city, Integer pageNum, Integer pageSize) {
		PageHelper.startPage(pageNum, pageSize);
		List<AgentInfo> list = agentInfoMapper.selectAll(showPublic,skillArea,researchArea, province, city);
		PageInfo pageInfo = new PageInfo(list);
		return pageInfo;
	}
	/**
	 * 根据用户主键或手机查看经纪人详情信息
	 * @param id 经纪人主键
	 * @param userInfoId 用户表主键
	 * @param phone 手机号码
	 * @return
	 */
    public AgentInfo selectDetailByUser(Long id,Long userInfoId,String phone){
    	return agentInfoMapper.selectDetailByUser(id,userInfoId, phone);
    }
    /**
   	 * 随机推荐经纪人
   	 * @param num 推荐数
   	 * @param skillArea 技术领域
	 * @param researchArea 关注行业
   	 * @return
   	 */
    public List<AgentInfo> recommendList(Integer num,String skillArea,String researchArea){
    	return agentInfoMapper.recommendList(num, skillArea,researchArea);
    }
    /**
	 * 格式化返回数据标签
	 * @return
	 */
    public AgentInfo formatLabel(AgentInfo agentInfo){
    	Map map = agentJobsService.sumScoreByUser(agentInfo.getId(), null, null, null);
		if(map != null)
		{
			Float avg = Float.parseFloat(map.get("avgScore").toString());
			Integer score = Integer.parseInt(map.get("score").toString());
			if(score == null)
				agentInfo.setScoreSum(0);
			else
				agentInfo.setScoreSum(score);
			if(avg == null)
				agentInfo.setScoreAvg(0);
			else
				agentInfo.setScoreAvg(Math.round(avg));
		}
		else
		{
			agentInfo.setScoreSum(0);
			agentInfo.setScoreAvg(0);
		}
		
    	if(!StringUtil.isEmpty(agentInfo.getUserInfoType()))
		{
			if("2".equals(agentInfo.getUserInfoType()))
			{
				UserInforCompany userInforCompany = userInfoService.selectUserInfoCompanyById(agentInfo.getUserInfoId().intValue());
				if(userInforCompany != null)
					agentInfo.setUserInfoName(userInforCompany.getCompanyName());
			}
		}
		if(!StringUtil.isEmpty(agentInfo.getCity()))
		{
			Region r = regionService.queryByRegionId(Integer.parseInt(agentInfo.getCity()));
			if(r != null)
				agentInfo.setCityLabel(r.getName());
		}
		if(!StringUtil.isEmpty(agentInfo.getProvince()))
		{
			Region r = regionService.queryByRegionId(Integer.parseInt(agentInfo.getProvince()));
			if(r != null)
				agentInfo.setProvinceLabel(r.getName());
		}
    	return agentInfo;
    }
}